import { ValidatorConstraint, ValidatorConstraintInterface, ValidationArguments } from 'class-validator';

@ValidatorConstraint({ name: 'passwordValidate', async: false })
export class PasswordValidate implements ValidatorConstraintInterface {

  // 密码长度6-64,并且满足(1个小写字母|1个大写字母|1个数字|1个特殊字符)中的两项
  validate(password: string, args: ValidationArguments) {
    if (/^.{6,64}$/.test(password) === false) { return false; }
    let len = 0; // 记录满足上述条件的个数
    if (/.*?[a-z]/.test(password)) { len++; }
    if (/.*?[A-Z]/.test(password)) { len++; }
    if (/.*?\d/.test(password)) { len++; }
    if (/.*?[~!@#\?\$%\^&\*\(\)-_=\+\|\[\{\}\];:]/.test(password)) { len++; }
    return len > 1;
  }

  defaultMessage(args: ValidationArguments) { // here you can provide default error message if validation failed
    return '密码长度6-64，需满足包含1个小写字母，1个大写字母，1个数字，1个特殊字符中的两项';
  }

}
